package com.lzh.seatrend.util.seatrend;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.NoSuchMessageException;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * @Classname MessageUtil
 * @Description MessageUtil工具类
 * @Date 2021/9/24 14:48
 * @Created by liuzihao
 */
@Component
public class MessageUtil {

    public static Logger logger = LoggerFactory.getLogger(MessageUtil.class);

    @Autowired
    protected static MessageSource messageSource;

    public MessageUtil(MessageSource messageSource) {
        MessageUtil.messageSource = messageSource;
    }

    public static String getDefaultMessage(String code, Locale locale, Object... args) {
        String message = null;
        try {
            message = messageSource.getMessage(code, args, locale);
        } catch (NoSuchMessageException e) {
            throw new MyRuntimeException("No message found under code '" + code + "' for properties");
        }
        return message;
    }
}
